package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.cqupt.cxcy.dto.InvestorImpl;
import cn.edu.cqupt.cxcy.model.Project;
import cn.edu.cqupt.cxcy.model.ProjectPage;

/**
 * Servlet implementation class InveDtxm
 */
@WebServlet("/inve/Dtxm")
public class InveDtxm extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InveDtxm() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String pjax_back = "$(function(){$(\"a.turn_pjax[href='inve/Dtxms']\").trigger('click');});";
		if(req.getHeader("x-pjax") == null || req.getHeader("x-pjax").equals("null")) {
			req.setAttribute("pjax_back", pjax_back);
			req.getRequestDispatcher("investor.jsp").forward(req, resp);
		} else {
			HttpSession session=req.getSession();
			List<Object> list=(List<Object>)session.getAttribute("user");
			int inve_id=(int)list.get(1);
		List<Project> show =new InvestorImpl().returnProjectByid(inve_id,2);
		
		 if(show.size()!=0)
		 {List<Map<String,String>> info=new ArrayList<Map<String,String>>();
		 int firstpagesum=1;
	 		if(show.size()%6==0)
	 			firstpagesum=show.size()/6;
	 		else
	 			firstpagesum=(show.size()/6)+1;
	 		ProjectPage page=new ProjectPage(firstpagesum,1,6);
           String pagenow=req.getParameter("pagenow");
  	    	
  	    	if(pagenow!=null)
  	    		page.setPagenow(Integer.parseInt(pagenow));
  	    	
  	    	String type=req.getParameter("type");
  	    	if(type!=null)
  	    	{if("up".equals(type))
  	    		{page.setPagenow(page.getPagenow()-1);
  	    		}
  	    	if("down".equals(type))
  	    			page.setPagenow(page.getPagenow()+1);
  	    		
  	    	}
  	    	
			 for(int i=(page.getPagenow()-1)*6;i<=6*page.getPagenow()-1;i++)
		 {Map<String,String> map=new HashMap<String,String>();
		 if(i<=show.size()-1)
		 { map.put("pro_id", Integer.toString(show.get(i).getP_id()));
		 map.put("pro_name",show.get(i).getP_name());
		 map.put("pro_acount", Integer.toString(new InvestorImpl().returnacount(inve_id, show.get(i).getP_id(),2)));
		 info.add(map); }
		 }
	
	     req.setAttribute("pageinfo", page);
		 req.setAttribute("pro_info", info);
		 
		 }
			req.getRequestDispatcher("tzxm.jsp").forward(req, resp);
		} 
	}

}
